#pragma once

#include "water/context.h"
#include "uart/ProtocolCustom.h"
#include "app/Activity.h"
#include "control/ZKListView.h"
#include "entry/EasyUIContext.h"
#include <base/strings.hpp>
#include "manager/Lock.h"
#include "logic/global_variables.h"
#include "utils/ScreenHelper.h"
#include "common.h"

#define INTENT_OPERATE			"operate"
#define INTENT_MODE       		"mode"
#define INTENT_ICON       		"icon"
#define INTENT_WATER_OUTPUT     "water_output"
#define INTENT_WATER_TEMP       "water_temp"
#define INTENT_NAME             "name"

#define RANGE_ITEMS(r)    ((r.max - r.min) / r.step + 1)
#define RANGE_VAL(r, i)   (r.min + (i) * r.step)
#define GET_RANGE_PROGRESS(min, max, val, items)	((val - min) * (items) / (max - min))

#ifndef DEBUG
#define DEBUG LOGD("--%d-- --%s-- Debug!!!\n", __LINE__, __FILE__);
#endif

#define COMPRESSORTIME (300)
//#define COMPRESSORTIME (5)
extern Inquiry_A2 Cur_a2;
// 用于执行系统命令。
#define SHELL(program) if (system(program)) {}

typedef struct {
	int operate;
	int mode;
	std::string name;
	const char *icon;
	int water_output;
	int water_temp;
} SBaseItem;

typedef enum {
	E_TIMER_EVENT_ID_ICE_ADD_WATER,					//冰水箱补水

	E_TIMER_EVENT_ID_DRAIN_WATER_SHORT,				//短时间排水
	E_TIMER_EVENT_ID_DRAIN_WATER_LONG,				//长时间排水
	E_TIMER_EVENT_ID_ALL_BRASH,						//整机冲洗
	E_TIMER_EVENT_ID_FILTER_BRASH,					//滤芯冲洗
	E_TIMER_EVENT_ID_ICE_ADD_AND_DRAIN_WATER,		//冰胆排换水
	E_TIMER_EVENT_ID_MAKE_WATER_TIMEOUT,			//外供进水
	E_TIMER_EVENT_ID_ZERO_STATE_WATER,				//零陈水
	E_TIMER_EVENT_ID_WATER_CHECK_PRESS_TEST,		//水检保压测试
	E_TIMER_EVENT_ID_WATER_OPERATE_HOT,			    //出水热水操作
	E_TIMER_EVENT_ID_WATER_OPERATE_OTHER,			//出水常温水冷水操作
} E_TIMER_EVENT_ID;


typedef enum {
	E_WARN_ID_LACK_WATER_HOT = 0,	//缺水(热水)
	E_WARN_ID_LACK_WATER_OTHER,		//缺水(其他)
	E_WARN_ID_INPUT_WATER_TEMP_SENSOR_ERROR,	//进水测温度故障
	E_WARN_ID_INPUT_WATER_TEMP_LOW,				//进水水温过低
	E_WARN_ID_OUTPUT_WATER_TEMP_SENSOR_ERROR,	//出水测温度故障
	E_WARN_ID_VOLTAGE_SENSOR_ERROR,				//电压传感器故障
	E_WARN_ID_HEAT_FILM_ERROR,					//发热膜回路故障
	E_WARN_ID_OVER_TEMP,						//超高温(H4)
	E_WARN_ID_ZERO_CIRCUIT_ERROR,				//过零电路故障(E7)
	E_WARN_ID_COMMUNICATION_ERROR,				//通讯故障
	E_WARN_ID_LEAK_ERROR,						//漏水故障
	E_WARN_ID_WATER_PUMP_OPEN,					//热水水泵开路故障
} E_WARN_ID;

static inline void open_app(const char *app_name, const SBaseItem &item) {
	Intent *intent = new Intent;

	intent->putExtra(INTENT_OPERATE, base::format("%d", item.operate));
	intent->putExtra(INTENT_MODE, base::format("%d", item.mode));
	intent->putExtra(INTENT_NAME, item.name);
	if (item.icon) {
		intent->putExtra(INTENT_ICON, item.icon);
	}
	intent->putExtra(INTENT_WATER_OUTPUT, base::format("%d", item.water_output));
	intent->putExtra(INTENT_WATER_TEMP, base::format("%d", item.water_temp));
	EASYUICONTEXT->openActivity(app_name, intent);
}

static inline bool get_intent_item(SBaseItem &item, const Intent *intentPtr) {
	if(intentPtr) {
		item.operate = atoi(intentPtr->getExtra(INTENT_OPERATE).c_str());
		item.mode = atoi(intentPtr->getExtra(INTENT_MODE).c_str());
		item.name = intentPtr->getExtra(INTENT_NAME);
		item.icon = intentPtr->getExtra(INTENT_ICON).c_str();
		item.water_output = atoi(intentPtr->getExtra(INTENT_WATER_OUTPUT).c_str());
		item.water_temp = atoi(intentPtr->getExtra(INTENT_WATER_TEMP).c_str());
		return true;
	}
	return false;
}


class TouchClass : public ZKBase::ITouchListener {
public:
    // 无参构造函数
    TouchClass();

    // 带参数的构造函数
    TouchClass(ZKTextView *textView, const LayoutPosition &pos, int value);

    //设置成员变量的值
    void setValues(ZKTextView *textView, const LayoutPosition &pos, int value,bool layout = true);

    // 重写触摸事件处理方法
    virtual void onTouchEvent(ZKBase *pBase, const MotionEvent &ev) override;

    // 设置进度为 maxValue * 百分比的位置
    void setProgressByPercentage(double percentage);

	private:
	    LayoutPosition layoutPos;
	    int maxValue;
	    ZKTextView *TextPtr;
};

//是否有冲突的警告和定时器
bool is_can_start_auto_timer(E_TIMER_EVENT_ID id);
//获取制冷是否开启
bool isColdOpen();
bool isIceOpen();
bool isCompressor();
//故障停水
bool stop_water(bool is_auto_stop = false);
//自定义蜂鸣次数
void set_beep_count(int count, int sleep_time = 500);

void showRefrigerWnd();		//是否开启制冷

void hideWaterWnd();		//返回主界面
//启用网络同步时间
int synchronization_time();


//一些按键操作方便直接使用
void operateBrash(bool on);

//出水出冰开关
bool operateIceCake(bool on);
bool operateWater(bool on);

//制冷制冰开关
bool operateCold(bool on);
bool operateIce(bool on);

//脱冰 清洗
void forceDefrost(bool is_act);
void internalClean(bool is_act);
void buttonClean(bool on);
void buttonForce(bool on);

void operateLock(bool on);

void operateEmptyDevice(bool on);

void operateResetDevice();

void shot();
